﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using PlaydeezGames.Common;

namespace IslandInterloper.Engine
{
    internal class Crewmember : GameClient
    {
        private string name;
        public string Name { get { return name; } }
        private CountedCollection<string> dailyConsumptions;
        public Dictionary<string, double>.KeyCollection DailyConsumptions
        {
            get
            {
                return dailyConsumptions.Keys;
            }
        }
        public double GetDailyConsumptionAmount(string commodity)
        {
            return dailyConsumptions[commodity];
        }
        private CountedCollection<string> skills;
        public Dictionary<string, double>.KeyCollection Skills
        {
            get
            {
                return skills.Keys;
            }
        }
        public double GetSkillValue(string skill)
        {
            return skills[skill];
        }
        private bool avatar;
        public bool Avatar { get { return avatar; } }
        private bool hireable;
        public bool Hireable { get { return hireable; } }
        private bool fireable;
        public bool Fireable { get { return fireable; } }
        private WeightedGenerator<int> crewGenerator;
        public int GenerateCrew()
        {
            return crewGenerator.Generate();
        }

        internal Crewmember(Game theGame, XElement element)
            : base(theGame)
        {
            FromXElement(element);
        }
        private void FromXElement(XElement element)
        {
            name = element.Element(GameXNames.name).Value;
            avatar = (element.Element(GameXNames.avatar) != null);
            hireable = (element.Element(GameXNames.hireable) != null);
            fireable = (element.Element(GameXNames.fireable) != null);
            dailyConsumptions = new CountedCollection<string>(element.Element(GameXNames.dailyConsumptions), Utilities.FromStringToString);
            skills = new CountedCollection<string>(element.Element(GameXNames.skills), Utilities.FromStringToString);
            crewGenerator = new WeightedGenerator<int>(element.Element(GameXNames.crewGenerator), Utilities.FromStringToInt);
        }
        public override XElement ToXElement(XName xName)
        {
            XElement result = new XElement(xName,
                new XElement(GameXNames.name,name),
                dailyConsumptions.ToXElement(GameXNames.dailyConsumptions),
                skills.ToXElement(GameXNames.skills),
                crewGenerator.ToXElement(GameXNames.crewGenerator)
                );
            if (avatar)
            {
                result.Add(new XElement(GameXNames.avatar));
            }
            if (hireable)
            {
                result.Add(new XElement(GameXNames.hireable));
            }
            if (fireable)
            {
                result.Add(new XElement(GameXNames.fireable));
            }
            return result;
        }
    }
}
